import Vue from "vue";
import App from "./App.vue";
import VueRouter from "vue-router";
import { routes } from "./routes/routes.js";
import Antd from "ant-design-vue";
import "ant-design-vue/dist/antd.css";
import store from "./vuex/index";
import echarts from "echarts";
import axios from "axios";
import { vueBaberrage } from 'vue-baberrage'
import less from 'less'

Vue.config.productionTip = false;
Vue.prototype.$echarts = echarts;

Vue.use(VueRouter);
Vue.use(Antd);
Vue.use(vueBaberrage)
Vue.use(less)

Vue.prototype.axios = axios;
Vue.config.productionTip = false;

const router = new VueRouter({
  routes,
  mode: "history",
});

// 导航守卫
router.beforeEach((to, from, next) => {
  console.log("🚀 ~ file: main.js ~ line 32 ~ router.beforeEach ~ sessionStorage.getItem('role')", sessionStorage.getItem("role"))
  console.log("🚀 ~ file: main.js ~ line 28 ~ router.beforeEach ~ from", from)
  console.log("🚀 ~ file: main.js ~ line 28 ~ router.beforeEach ~ to", to)
  if (to.name == "index" || to.name == "failResult") {
    next()
    return;
  }
  if (!sessionStorage.getItem("role")) {
    next(
      {
        name: "index"
      }
    )
    return;
  }
  // 老师路由判定
  if (to.path.includes("/teacher/") || to.path.includes("/interaction")) {
    if (sessionStorage.getItem("role") == "teacher") {
      next();
      return;
    } else {
      // 此处显示没权限页面!
      next({
        name: "failResult"
      })
      return;
    }
  }
  // 管理员路由
  if (to.path.includes("/admin/")) {
    if (sessionStorage.getItem("role") == "admin") {
      next();
      return;
    } else {
      // 此处显示没权限页面!
      next({
        name: "failResult"
      })
      return;
    }
  }
  // 超级管理员路由
  if (to.path.includes("/supervisor/")) {
    if (sessionStorage.getItem("role") == "super") {
      next();
      return;
    } else {
      // 此处显示没权限页面!
      next({
        name: "failResult"
      })
      return;
    }
  }
  // 巡课路由
  if (to.path.includes("/patrol")) {
    if (sessionStorage.getItem("role") == "patrol") {
      next();
      return;
    } else {
      // 此处显示没权限页面!
      next({
        name: "failResult"
      })
      return;
    }
  }
  // 学生路由
  if (to.path.includes("/student")) {
    if (sessionStorage.getItem("role") == "user") {
      next();
      return;
    } else {
      // 此处显示没权限页面!
      next({
        name: "failResult"
      })
      return;
    }
  }
})

new Vue({
  store,
  router,
  render: (h) => h(App),
}).$mount("#app");
